﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using blackhouse.Data;

namespace blackhouse.BlackApart.Data.Sql {
    public class OpinionsDataAccess : AdoOpinionsDataAccess {

        #region Fields

        private string connectionString;
        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public OpinionsDataAccess(string sqlConnectionString) {
            this.connectionString = sqlConnectionString;
            this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
            SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
        }

        #endregion

        #region AdoFurnitureDataAccess Members

        public override IDataReader GetReaderForOpinionsCategories(string language) {
            this.dataHelper.ProcedureName = "bhp_OpinionsCategoriesGetAll";
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForOpinion(int opinionId) {
            this.dataHelper.ProcedureName = "bhp_OpinionsGet";
            this.dataHelper.AddParameter("OpinionId", opinionId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override int SaveOpinion(int opinionId, string inPlusOpinion, string inMinusOpinion, DateTime publishedDate, string publisherCategory, string publisherFirstName, string publisherLastName, string publisherCountry, string language, int userId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsSave";
            this.dataHelper.AddParameter("Id", opinionId, DbType.Int32);
            this.dataHelper.AddParameter("InPlusOpinion", inPlusOpinion, DbType.String);
            this.dataHelper.AddParameter("InMinusOpinion", inMinusOpinion, DbType.String);
            this.dataHelper.AddParameter("PublishedDate", publishedDate, DbType.DateTime);
            this.dataHelper.AddParameter("AuthorCategory", publisherCategory, DbType.String);
            this.dataHelper.AddParameter("AuthorFirstName", publisherFirstName, DbType.String);
            this.dataHelper.AddParameter("AuthorLastName", publisherLastName, DbType.String);
            this.dataHelper.AddParameter("AuthorCountry", publisherCountry, DbType.String);
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
            if (userId > 0)
                this.dataHelper.AddParameter("UserId", userId, DbType.Int32);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public override int SaveModerateOpinion(int opinionId, string inPlusOpinion, string inMinusOpinion, string answer, int userId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsModerateSave";
            this.dataHelper.AddParameter("Id", opinionId, DbType.Int32);
            this.dataHelper.AddParameter("InPlusOpinion", inPlusOpinion, DbType.String);
            this.dataHelper.AddParameter("InMinusOpinion", inMinusOpinion, DbType.String);
            this.dataHelper.AddParameter("Answer", answer, DbType.String);
            if (userId > 0)
                this.dataHelper.AddParameter("UserId", userId, DbType.Int32);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public override void AddOpinionDetailValue(int opinionId, int categoryId, float val) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsDetailSave";
            this.dataHelper.AddParameter("OpinionId", opinionId, DbType.Int32);
            this.dataHelper.AddParameter("OpinionCategoryId", categoryId, DbType.Int32);
            this.dataHelper.AddParameter("Value", val, DbType.Single);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override bool AddOpinionToReservation(int opinionId, int reservationId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsReservationAdd";
            this.dataHelper.AddParameter("OpinionId", opinionId, DbType.Int32);
            this.dataHelper.AddParameter("ReservationId", reservationId, DbType.Int32);
            return this.dataHelper.ExecuteWithoutResult() > 0;
        }

        public override IDataReader GetReaderForOpinionDetails(int opinionId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsDetailsGet";
            this.dataHelper.AddParameter("OpinionId", opinionId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForStatisticOpinion() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsStatistics";
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForOpinionsDetails(bool includeNonPublic) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsDetailsGetValues";
            this.dataHelper.AddParameter("IncludeNonPublic", includeNonPublic, DbType.Boolean);
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForOpinionsHeaders(bool includeNonPublic) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsHeadersGet";
            this.dataHelper.AddParameter("IncludeNonPublic", includeNonPublic, DbType.Boolean);
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForOpinionTextValues(string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsStatisticsText";
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength);
            return this.dataHelper.Execute();
        }

        public override void PublishOpinion(int opinionId, int userId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsPublish";
            this.dataHelper.AddParameter("OpinionId", opinionId, DbType.Int32);
            if (userId > 0)
                this.dataHelper.AddParameter("UserId", userId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override int FindReservationToAddOpinion(int apartmentTypeId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_OpinionsFindFreeReservation";
            this.dataHelper.AddParameter("ApartmentTypeId", apartmentTypeId, DbType.Int32);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        #endregion

    }
}
